package com.zhengsr.latte.net;

import android.content.Context;
import android.view.View;

import com.zhengsr.latte.ui.loader.LatteLoader;
import com.zhengsr.latte.net.callback.RequestCallBack;
import com.zhengsr.latte.net.callback.RestListenerAdapter;
import com.zhengsr.latte.net.download.DownloadHanlder;

import java.io.File;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by zhengshaorui on 2018/3/22.
 * 这个是真正对外公布的方法
 */

public class RestClient {
    private static final String TAG = "RestClient";
    //private static final WeakHashMap<String, Object> PARMAS = RestCreator.getParams();
    private Builder mBuilder;

    public RestClient(Builder builder) {
        mBuilder = builder;
    }


    public void request(HttpMethod method) {
        RestService service = RestCreator.getService();
        retrofit2.Call<String> call = null;
        if (mBuilder != null) {
            if (mBuilder.listenerAdapter != null) {
                mBuilder.listenerAdapter.onRequestStart();
            }
            if (mBuilder.dialogview != null && mBuilder.context != null){
                LatteLoader.showDialog(mBuilder.context,mBuilder.dialogview);
            }

            switch (method) {
                case GET:
                    call = service.get(mBuilder.url, mBuilder.params);
                    break;
                case POST:
                    call = service.post(mBuilder.url, mBuilder.params);
                    break;
                case POST_RAW:
                    call = service.postRaw(mBuilder.url, mBuilder.body);
                    break;
                case PUT:
                    call = service.put(mBuilder.url, mBuilder.params);
                    break;
                case PUT_RAW:
                    call = service.putRaw(mBuilder.url, mBuilder.body);
                    break;
                case DELETE:
                    call = service.delete(mBuilder.url, mBuilder.params);
                    break;
                case UPLOAD:
                    final RequestBody requestBody = RequestBody.
                            create(MediaType.parse(MultipartBody.FORM.toString()),mBuilder.file);
                    final MultipartBody.Part body = MultipartBody.
                            Part.createFormData("file",mBuilder.file.getName(),requestBody);
                    call = service.upload(mBuilder.url,body);
                    break;

                default:
                    break;
            }
            if (call != null) {
                call.enqueue(new RequestCallBack(mBuilder));
            }
        }

    }


    public RestClient get() {
        request(HttpMethod.GET);
        return this;
    }

    public RestClient post() {
        if (mBuilder != null){
            if ( mBuilder.body == null){
                request(HttpMethod.POST);
            }else{
                if (mBuilder.params != null) {
                    if (!mBuilder.params.isEmpty()) {
                        throw new RuntimeException("params must be null");
                    }
                }
                request(HttpMethod.POST_RAW);
            }
        }
        return this;
    }

    public RestClient delete() {
        request(HttpMethod.DELETE);
        return this;
    }

    public RestClient put() {
        if (mBuilder != null){
            if ( mBuilder.body == null){
                request(HttpMethod.PUT);
            }else{
                if (mBuilder.params != null) {
                    if (!mBuilder.params.isEmpty()) {
                        throw new RuntimeException("params must be null");
                    }
                }
                request(HttpMethod.PUT_RAW);
            }
        }
        return this;
    }

    public RestClient upload(){
        request(HttpMethod.UPLOAD);
        return this;
    }


    public RestClient download(){
        new DownloadHanlder(mBuilder);
        return this;
    }


    public static class Builder {
        public RestListenerAdapter listenerAdapter;
        public String url;
        public RequestBody body;
        public Context context;
        public View dialogview;
        public String downloadDir;
        public String extension;
        public String name;
        private File file;
        public WeakHashMap<String,Object> params = new WeakHashMap<>();


        public Builder setDownloadDir(String downloadDir) {
            this.downloadDir = downloadDir;
            return this;
        }

        public Builder setExtension(String extension) {
            this.extension = extension;
            return this;
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }


        public Builder setListenerAdapter(RestListenerAdapter listenerAdapter) {
            this.listenerAdapter = listenerAdapter;
            return this;
        }

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setParams(WeakHashMap<String, Object> params) {
            params.putAll(params);
            return this;
        }

        public Builder setParams(String key, Object value) {
            params.put(key, value);
            return this;
        }

        public Builder setFile(File file) {
            this.file = file;
            return this;
        }

        public Builder setFile(String filePath) {
            this.file = new File(filePath);
            return this;
        }

        public Builder setRaw(String raw) {
            this.body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
            return this;
        }

        public Builder setDialog(Context context, View dialogview) {
            this.context = context;
            this.dialogview = dialogview;
            return this;
        }

        public RestClient builder() {
            return new RestClient(this);
        }

    }


}
